package com.swj.common.core.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.swj.common.core.annotation.Excel;
import com.swj.common.core.annotation.Excels;
import com.swj.common.core.constant.UserConstants;
import com.swj.common.core.model.web.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * @Description 系统用户实体
 * @author Sans
 * @CreateTime 2019/6/14 15:57
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_user")
public class SysUser extends BaseEntity {

    /**
     * 部门ID
     */
    @Excel(name = "部门编号", type = Excel.Type.IMPORT , needMerge = true)
    private String deptId;

    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @Size(min = UserConstants.USERNAME_MIN_LENGTH,
            max = UserConstants.USERNAME_MAX_LENGTH,
            message = "用户名必须在2~20字符之间个字符")
    @Excel(name = "用户名称" , needMerge = true)
    private String userName;

    /**
     * 用户昵称
     */
    @Excel(name = "用户昵称" , needMerge = true)
    private String nickName;

    /**
     * 用户类型
     */
    @Excel(name = "用户类型" , needMerge = true)
    private String userType;


    /**
     *  密码
     */
    @NotBlank(message = "密码不能为空")
    @Size(min = UserConstants.PASSWORD_MIN_LENGTH,
            max = UserConstants.PASSWORD_MAX_LENGTH,
            message = "用户名必须在5~20字符之间个字符")
    @Excel(name = "初试密码",type = Excel.Type.IMPORT)
    private String password;

    /**
     * 用户邮箱
     */
    @Excel(name = "用户邮箱" , needMerge = true)
    private String email;

    /**
     * 手机号码
     */
    @Excel(name = "手机号码" , needMerge = true)
    private String phoneNumber;

    /**
     * 用户性别
     */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知" , needMerge = true)
    private String sex;

    /**
     * 账号状态（0正常 1停用）
     */
    @Excel(name = "帐号状态", readConverterExp = "1=正常,0=停用" , needMerge = true)
    private String status;


    /**
     * 是否管理员 0:不是.1:是
     */
    @TableField(exist = false)
    private boolean admin = false;

    /**
     * 最后登录IP
     */
    @Excel(name = "最后登录IP", type = Excel.Type.EXPORT , needMerge = true)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @Excel(name = "最后登录时间", dateFormat = "yyyy-MM-dd HH:mm:ss", type = Excel.Type.EXPORT , needMerge = true)
    private LocalDateTime loginTime;

    /**
     * 登录过的ip
     */
    @Excel(name = "登录过的ip", type = Excel.Type.EXPORT , needMerge = true)
    private String loginedIp;


    /**
     * 备注
     */
    @Excel(name = "备注" , needMerge = true)
    private String remark;

    /** 部门对象 */
    @Excels({
            @Excel(name = "部门名称",    targetAttr = "deptName",type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "部门负责人",  targetAttr = "leader",  type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "部门联系电话",targetAttr = "phone",  type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "部门邮箱",   targetAttr = "email",   type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "部门状态",   targetAttr = "status",  type = Excel.Type.EXPORT, needMerge = true,readConverterExp = "1=正常,0=停用")
    })
    @TableField(exist = false)
    private SysDept dept;

    /**
     * 岗位对象
     */
    @Excels({
            @Excel(name = "岗位编码", targetAttr = "postCode", type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "岗位名称", targetAttr = "postName", type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "岗位备注", targetAttr = "remark",   type = Excel.Type.EXPORT, needMerge = true),
            @Excel(name = "岗位状态", targetAttr = "status",   type = Excel.Type.EXPORT, needMerge = true, readConverterExp = "1=正常,0=停用")
    })
    @TableField(exist = false)
    private SysPost post;

    /**
     * 角色对象
     */
    @TableField(exist = false)
    @Excel(name = "角色",  type = Excel.Type.EXPORT)
    private Set<SysRole> roles;

    /** 角色组 */
    @TableField(exist = false)
    private String[] roleIds;

    /** 岗位id */
    @TableField(exist = false)
    private String postId;


    public static boolean isAdmin(String userId) {
        return "1" .equals( userId );
    }

    public boolean isAdmin() {
        return "1" .equals( this.id );
    }

    public SysUser(String id) {
        super(id);
    }
}
